﻿Public Class frmTaxBR
    Private frmParent As frmMain
    Dim validator As PropertyValidator = New PropertyValidator
    'Dim sqlManager As New SQLManager
    'Dim sqlList As New SQLList
    Dim payrollManager As New PayrollManager
    Private Sub frmTaxBR_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.MdiParent = frmMain
        ClearAllFields()
        Try
            'Dim taxList As List(Of Tax) = sqlList.getAllTax()
            Dim taxList As List(Of Tax) = payrollManager.getAllTax()
            For Each tax As Tax In taxList
                Dim item As ListViewItem
                Dim taxCodeList As List(Of TaxCode) = payrollManager.getItemTaxCodeByID(tax.tax_code_id)
                For Each taxCode As TaxCode In taxCodeList
                    item = lvwTax.Items.Add(taxCode.tax_code_desc)
                    item.SubItems.Add(tax.min_salary)
                    item.SubItems.Add(tax.max_salary)
                    item.SubItems.Add(tax.base_tax)
                    item.SubItems.Add(tax.percentage_over)
                    item.SubItems.Add(tax.id)
                Next
                'item = lvwTax.Items.Add(tax.tax_code_id)
                'item.SubItems.Add(tax.min_salary)
                'item.SubItems.Add(tax.max_salary)
                'item.SubItems.Add(tax.base_tax)
                'item.SubItems.Add(tax.percentage_over)
                'item.SubItems.Add(tax.id)
            Next
            grpTax.Text = "Total Record(s): " + lvwTax.Items.Count.ToString()
            validator.resizes(lvwTax)
        Catch ex As Exception
            MsgBox("Can't load Web page" & vbCrLf & ex.Message)
        End Try
    End Sub
    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        frmTaxDE.ShowDialog()
        frmTaxBR_Load(vbNull, EventArgs.Empty)
    End Sub
    Public Function ClearAllFields()
        validator.ResetControls(grpTax)
        btnAdd.Enabled = True
        btnEdit.Enabled = False
        btnDelete.Enabled = False
        btnImport.Enabled = True
        grpTax.Text = "Total Record(s): 0"
        validator.resizes(lvwTax)
        Clipboard.Clear()
        Return 0
    End Function
    Private Sub btnImport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImport.Click
        Clipboard.SetText("Tax")
        frmImport.ShowDialog()
        frmTaxBR_Load(vbNull, EventArgs.Empty)
    End Sub
    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Try
            For i As Integer = 1 To lvwTax.SelectedItems.Count
                'sqlManager.executeQuery("DELETE FROM tbl_tax WHERE id = " + lvwTax.SelectedItems(i - 1).SubItems(5).Text)
                payrollManager.deleteItemTax(lvwTax.SelectedItems(i - 1).SubItems(5).Text)
            Next
            MsgBox("Record(s) has been deleted.")
            frmTaxBR_Load(vbNull, EventArgs.Empty)
            grpTax.Text = "Total Record(s): " + lvwTax.Items.Count.ToString()
        Catch ex As Exception
            MsgBox("Can't load Web page" & vbCrLf & ex.Message)
        End Try
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        Clipboard.SetText(lvwTax.SelectedItems(0).SubItems(5).Text)
        frmTaxDE.ShowDialog()
        frmTaxBR_Load(vbNull, EventArgs.Empty)
    End Sub

    Private Sub lvwTax_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvwTax.SelectedIndexChanged
        btnEdit.Enabled = (lvwTax.SelectedItems.Count = 1)
        btnDelete.Enabled = (lvwTax.SelectedItems.Count > 0)
    End Sub
End Class